<?php

namespace IDock\Insurance\LIHI\Response;

use IDock\Http\Response;
use IDock\IDock\IDockEnums;
use IDock\Insurance\LIHI\LIHIEnums;
use IDock\Insurance\LIHI\Response\AllLIHIResponse;
use IDock\Printer\VehicleModelPrinter;
use IDock\Printer\VehiclePrinter;

class VehicleModelResponse extends AllLIHIResponse
{

    /**
     * @var VehicleModelPrinter $printer
     */
    public $printer = VehicleModelPrinter::class;

    public function printerInit(): Response
    {
        $body = $this->getBody();
        $modelList = $body['result']['motorList'] ?? [];
        if (!$modelList) {
            return $this;
        }

        foreach ($modelList as $datum) {

            $vehiclePrinter = new VehiclePrinter();
            $vehiclePrinter->vehicleModelCode = $datum['modeCode'] ?? '';
            $vehiclePrinter->brandName = $datum['modelName'] ?? '';
            $vehiclePrinter->vehicleExhaust = $datum['exHaustScale'] ?? '';
            $vehiclePrinter->item['brandName'] = $datum['carBrand'] ?? '';

            //进口车标识
            if ($datum['countryCode'] == '国产车') {
                $vehiclePrinter->vehicleImport = 'I1';
                $vehiclePrinter->vehicleImportName = IDockEnums::$importFlag[$vehiclePrinter->vehicleImport];
            }
            if ($datum['countryCode'] == '合资车') {
                $vehiclePrinter->vehicleImport = 'I2';
                $vehiclePrinter->vehicleImportName = IDockEnums::$importFlag[$vehiclePrinter->vehicleImport];
            }
            if ($datum['countryCode'] == '进口车') {
                $vehiclePrinter->vehicleImport = 'I3';
                $vehiclePrinter->vehicleImportName = IDockEnums::$importFlag[$vehiclePrinter->vehicleImport];
            }

            $vehiclePrinter->vehicleName = $datum['modelName'] ?? '';
            $vehiclePrinter->vehiclePrice = $datum['purchasePrice'] ?? '';
            $vehiclePrinter->vehiclePriceWithOutTax = $datum['vehiclePurchasePrice'] ?? '';
            $vehiclePrinter->vehicleSeat = $datum['seatCount'] ?? '';
            $vehiclePrinter->remark = $datum['remark'] ?? '';
            $vehiclePrinter->vehicleYear = $datum['carYear'] ?? '';
            $tonCount = $datum['tonCount'] ?? 0;
            $vehiclePrinter->vehicleTonnage = $tonCount * 1000;
            $vehiclePrinter->wholeWeight = $datum['curbWeight'] ?? '0';

            //能源类型
            $powerTypeCode = $datum['powertypecode'] ?? '0';
            $vehiclePrinter->energyTypeCode = LIHIEnums::$energyType[LIHIEnums::$LBEnergyType[$powerTypeCode] ?? '4'];
            $vehiclePrinter->energyTypeName = IDockEnums::$energyType[$vehiclePrinter->energyTypeCode];
            //新能源
            if (in_array($vehiclePrinter->energyTypeCode,['E02','E03','E04'])) {
                $vehiclePrinter->isNewEnergy = '1';
            }
            $vehiclePrinter->item['flowId'] = $body['result']['flowId'] ?? '';
            $this->printer->setModelList($vehiclePrinter);
        }

        return $this;
    }
}
